package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 三、团体智力测验（Group）共60题，有一题是输入内容
 */
public class Table3 extends AbstractTable implements TableCalculate{
    String code = "Group";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetGroupFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return GetGroupReport(map);
    }

    // ☆三、团体智力测验（Group）共60题 ****，有一题是输入内容
    private static ClsAnswerResult GetGroupFactorScore(ClsAnswerRequest answer) {
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 60;
            // 题目的正确答案
            String strTemp = "3,5,1,3,3,5,4,5,2,1,7,3,4,3,4,3,3,3,3,4,5,4,2,3,5,1,4,3,4,3,2,4,4,4,3,3,5,@月,2,3,4,3,4,1,4,2,4,2,5,2,1,4,4,2,3,2,3,1,5,3";
            String[] strAnswer_s = strTemp.split(",");

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i;
            int vFactor = 0;

            // 总分
            for (i = 0; i < sAnswer.length; i++) {
                if (sAnswer[i].equalsIgnoreCase(strAnswer_s[i])) {
                    vFactor += 1;
                }
            }

            // 按“总分”得到“智商数”放入“诊断结果”
            // 年龄，最小值，最大值，中间值
            String vAppraise = "";
            int[][] intValueA = { { 11, 8, 46, 7 }, { 12, 10, 48, 9 }, { 13, 13, 51, 12 }, { 14, 15, 53, 14 },
                    { 15, 17, 55, 17 }, { 100, 19, 58, 18 } };
            for (i = 0; i < intValueA.length; i++) {
                if (answer.getAge() <= intValueA[i][0]) {
                    if (vFactor >= intValueA[i][1] && vFactor <= intValueA[i][2]) {
                        vAppraise = String.valueOf(82 + (vFactor - intValueA[i][3]) * 2);
                    } else if (vFactor < intValueA[i][1]) {
                        vAppraise = "80以下";
                    } else if (vFactor > intValueA[i][2]) {
                        vAppraise = "165以上";
                    }
                    break;
                }
            }

            ClsScore.Group group = new ClsScore.Group();
            group.setCorrectAmount_o(vFactor);
            group.setIQ_o(vAppraise);

            result.setObject(group);
            return result;

        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }

    // 三、团体智力测验Group
    private String[] GetGroupReport(Map map) {
        // 下面因子名称字段改成.toLowerCase()小写，是因为JAVA转json的时候会把大写字母转成小写字母
        // 第一个，错误信息；第二个，量表得分部分；第三个，量表指导意见_d数组部分
        String[] strResult = { "", "", "", "", "" };
        // String[] strResult = {"","",""};
        try {
            int i = 0;
            String[] gcName = "正确题数(共60题),智商数".split(",");
            String[] geName = "correctAmount,iQ".split(",");

            if (g_inblFactor) // 因子得分
            {

                strResult[1] += "<table cellpadding='0' cellspacing='0' width='100%' border='0'>";

                for (i = 0; i < gcName.length; i++) {
                    strResult[1] += "<tr><td" + subTitle(0, tWidth * 2) + ">" + gcName[i] + "</td>" + "<td"
                            + subValue(0, -1) + ">" + map.get(geName[i] + "_o") + "</td></tr>";
                }

                strResult[1] += "</table>";
            }

            strResult[3] = "0";
            strResult[4] = "";

            return strResult;
        } catch (ArithmeticException e) {
            strResult[0] = "失败";
            return strResult;
        }
    }

}
